
//
//  HotCollectViewController.m
//  BeautifulShoppings
//
//  Created by GivenBmli on 16/9/14.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "HotCollectViewController.h"
#import "HotCollectCollectionViewCell.h"
#import "ToolDB.h"
#import "ToolModel.h"
#import "UIImageView+WebCache.h"
#import "HotDetailsViewController.h"
#import "HotDatailCollectViewController.h"
#import "TAMacro.h"

@interface HotCollectViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic ,strong) UICollectionView *collectionView;
@property (nonatomic ,strong)NSMutableArray * array;
@property (nonatomic,strong)UIButton * like_Button;
@property(nonatomic,assign) enum CellState;

@end

@implementation HotCollectViewController
- (NSMutableArray *)array {
    
    if (!_array) {
        self.array = [ToolDB selectFromSql:@"SELECT title,cover_image_url,Id FROM t_Hot"];
        
        if (self.array.count == 0) {
            UIImageView *image  = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
            image.backgroundColor = [UIColor redColor];
            image.image = [UIImage imageNamed:@"文字2.jpg"];
            [self.view addSubview:image];
            UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 4  , SCREEN_HEIGHT / 10, SCREEN_WIDTH , SCREEN_HEIGHT / 10)];
            lable.text = @"您还未收藏任何东西";
            lable.font = [UIFont systemFontOfSize:20];
            lable.textColor = [UIColor whiteColor];
            [self.view addSubview:lable];
        }
    }
    
    return _array;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"热门收藏";
    [self foundCollectionView];
    UIButton *butt = [UIButton buttonWithType:UIButtonTypeCustom];
    [butt setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    butt.frame  = CGRectMake(0, 0, 50, 50);
    UIBarButtonItem *left= [[UIBarButtonItem alloc]initWithCustomView:butt];
    [butt addTarget:self action:@selector(clickLeft:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem =left;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [self nightModel];
    
    
}
- (void)foundCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH/2 - 15, SCREEN_HEIGHT /2.44);
    flowLayout.sectionInset = UIEdgeInsetsMake(0 , 5, 5, 5);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView jxl_setDayMode:^(UIView *view) {
        self.collectionView.backgroundColor = [UIColor whiteColor];
    } nightMode:^(UIView *view) {
        self.collectionView.backgroundColor = [UIColor grayColor];
    }];
    [self.collectionView registerClass:[HotCollectCollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
    [self.view addSubview:_collectionView];
   
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.array.count;
}
- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HotCollectCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    [cell jxl_setDayMode:^(UIView *view) {
        cell.backgroundColor =  [UIColor colorWithRed:0.939 green:0.980 blue:0.939 alpha:1.000];
    } nightMode:^(UIView *view) {
        cell.backgroundColor =  [UIColor grayColor];
    }];
    self.model = self.array[indexPath.row];
    
    cell.name.text = self.model.title;
    
    [cell.cover_image_url sd_setImageWithURL:[NSURL URLWithString:self.model.cover_image_url] placeholderImage:[UIImage imageNamed:@"站位"]];
    cell.id.text = self.model.Id;
    
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    HotDatailCollectViewController  *HotDetails = [[HotDatailCollectViewController alloc]init];
    //    // 属性传值第3步
    self.model = self.array[indexPath.item];
    self.hidesBottomBarWhenPushed = YES;
    NSInteger num = [self.model.Id integerValue];
    HotDetails.str =num;
    [self.navigationController pushViewController:HotDetails animated:YES];
    
    self.hidesBottomBarWhenPushed = NO;
    
}
- (void)clickLeft:(UIButton*)qwe {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)nightModel {
    //设置日间和夜间两张状态
    [self.view jxl_setDayMode:^(UIView *view) {
        //设置日间模式
        view.backgroundColor = [UIColor whiteColor];
        
    } nightMode:^(UIView *view) {
        view.backgroundColor = [UIColor colorWithRed:0.12 green:0.14 blue:0.15 alpha:1.00];
    }];
    [self.navigationController.navigationBar jxl_setDayMode:^(UIView *view) {
        UINavigationBar *bar = (UINavigationBar *)view;
        //改变状态栏前景颜色
        bar.barStyle = UIBarStyleDefault;
        bar.barTintColor = [UIColor colorWithRed:0.98 green:0.29 blue:0.31 alpha:1.00];
        //        bar.barTintColor = [UIColor redColor];
    } nightMode:^(UIView *view) {
        UINavigationBar *bar = (UINavigationBar *)view;
        //改变状态栏前景颜色
        bar.barStyle = UIBarMetricsDefault;
        bar.barTintColor = [UIColor colorWithRed:0.12 green:0.14 blue:0.15 alpha:1.00];
    }];
    
}
@end
